﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using PPI.Entities;
using PPI.Entities.Enumerator;
using PPI.Entities.Inspections;
using PPI.Entities.Lookup;
using PPI.Entities.Settings.Mobile;
using PPI.Entities.XML;
using PPI.General;
using PPI.Mobile.Logic;
using VMind.Global;

namespace PPI.Client.Logic.Mobile
{
    public class LMobileSettings
    {
        public static EMobileSettingsBase LoadMobileSettings(string connectionString)
        {
            EMobileSettingsBase returnSettings  = null;
            StringBuilder _query = new StringBuilder();

            #region Query
            _query.AppendLine("Select * from tb_Settings");
            #endregion

            EXmlOutboxBase baseOutbox =  LOutbox.GetXmlOutboxBase(connectionString);
            DataTable dt = LMobileDatabase.ExecuteQueryDataset(_query.ToString(), connectionString).Tables[0];

            switch(baseOutbox.DatabaseType)
            {
                case DatabaseTypes.GPS:
                    returnSettings = ParseGpsSettings(dt);
                    break;
                case DatabaseTypes.Inspection:
                    returnSettings = ParseInspectionSettings(dt);
                    break;
            }
            return returnSettings;
        }
        private static EMobileSettingsBase ParseGpsSettings(DataTable dt)
        {
            EMobileSettingsGPS gpsSettings = new EMobileSettingsGPS();
            foreach (DataRow dr in dt.Rows)
            {
                int settingKey = ConvertHelper.ToInt32(dr["SettingKey"]);
                EnumMobileSettings settingEnum =
                    (EnumMobileSettings) Enum.ToObject(typeof (EnumMobileSettings), settingKey);
                

                switch (settingEnum)
                {
                    case EnumMobileSettings.Handheld:
                        gpsSettings.Handheld =
                            new EHandheld(ConvertHelper.ToInt32(dr["SettingValue"]), dr["SettingName"].ToString());
                        break;
                    case EnumMobileSettings.MobileTypeDatabase:
                        break;
                    case EnumMobileSettings.MobileVersion:
                        break;
                    case EnumMobileSettings.Project:
                        gpsSettings.Project =
                            new EVendor_Project(ConvertHelper.ToInt32(dr["SettingValue"]), dr["SettingName"].ToString());
                        break;
                    case EnumMobileSettings.User:
                        gpsSettings.Contact =
                            new EVendor_Contact(ConvertHelper.ToInt32(dr["SettingValue"]), dr["SettingName"].ToString());
                        break;
                    case EnumMobileSettings.Utility:
                        gpsSettings.Utility =
                            new EUtility(ConvertHelper.ToInt32(dr["SettingValue"]), dr["SettingName"].ToString());
                        break;
                    case EnumMobileSettings.Vendor:
                        gpsSettings.Vendor =
                            new EVendor(ConvertHelper.ToInt32(dr["SettingValue"]), dr["SettingName"].ToString(), "");
                        break;
                }
            }


            return gpsSettings;
        }
        private static EMobileSettingsBase ParseInspectionSettings(DataTable dt)
        {
            EMobileSettingsInspection inspectionSettings = new EMobileSettingsInspection();
            foreach (DataRow dr in dt.Rows)
            {
                int settingKey = ConvertHelper.ToInt32(dr["SettingKey"]);
                EnumMobileSettings settingEnum =
                    (EnumMobileSettings) Enum.ToObject(typeof (EnumMobileSettings), settingKey);


                switch (settingEnum)
                {
                    case EnumMobileSettings.Handheld:
                        inspectionSettings.Handheld =
                            new EHandheld(ConvertHelper.ToInt32(dr["SettingValue"]), dr["SettingName"].ToString());
                        break;
                    case EnumMobileSettings.Company:
                        inspectionSettings.Company = new ECompany(ConvertHelper.ToInt32(dr["SettingValue"]), dr["SettingName"].ToString());
                        break;
                    case EnumMobileSettings.Inspector:
                        inspectionSettings.Inspector = new EInspector(ConvertHelper.ToInt32(dr["SettingValue"]), dr["SettingName"].ToString());
                        break;
                    case EnumMobileSettings.Project:
                        inspectionSettings.Project =
                            new EVendor_Project(ConvertHelper.ToInt32(dr["SettingValue"]), dr["SettingName"].ToString());
                        break;
                    case EnumMobileSettings.User:
                        inspectionSettings.Contact =
                            new EVendor_Contact(ConvertHelper.ToInt32(dr["SettingValue"]), dr["SettingName"].ToString());
                        break;
                    case EnumMobileSettings.Vendor:
                        inspectionSettings.Vendor =
                            new EVendor(ConvertHelper.ToInt32(dr["SettingValue"]), dr["SettingName"].ToString(), "");
                        break;

                }
            }


            return inspectionSettings;


            //ConvertHelper.ToInt32(dr["SettingValue"]), dr["SettingName"].ToString()


            //return baseSettings;
        }
    }
}
